package com.chenfan.mcn.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenfan.mcn.config.UserVoContextHolder;
import com.chenfan.mcn.constant.McnConstant;
import com.chenfan.mcn.constant.NumberCst;
import com.chenfan.mcn.dao.StarArchivingCodeRuleMapper;
import com.chenfan.mcn.dao.StarBasicDataMapper;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.model.McnPage;
import com.chenfan.mcn.model.StarArchivingCodeRule;
import com.chenfan.mcn.model.StarBasicData;
import com.chenfan.mcn.model.dto.StarArchivingCodeRuleSaveDTO;
import com.chenfan.mcn.model.vo.StarArchivingCodeRuleVO;
import com.chenfan.mcn.service.StarArchivingCodeRuleService;
import com.chenfan.mcn.utils.BusinessAssert;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 特殊红人归档编码规则 服务实现类
 * </p>
 *
 * @author wulg
 * @since 2021-12-17
 */
@Service
public class StarArchivingCodeRuleServiceImpl implements StarArchivingCodeRuleService {

    @Autowired
    private StarArchivingCodeRuleMapper ruleMapper;

    @Autowired
    private StarBasicDataMapper starBasicDataMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public boolean save(StarArchivingCodeRuleSaveDTO saveDTO) {
        StarBasicData starBasicData = starBasicDataMapper.selectOne(Wrappers.lambdaQuery(StarBasicData.class)
                .eq(StarBasicData::getId, saveDTO.getStarId())
                .eq(StarBasicData::getIsDelete, NumberCst.ZERO));
        BusinessAssert.notNull(starBasicData, McnErrorCode.DATA_NOT_FOUND, "查询不到该红人");
        BusinessAssert.isTrue(NumberCst.ZERO.equals(starBasicData.getStarStatus()), McnErrorCode.PARAM_ERROR, "该红人尚未启用");
        BusinessAssert.isTrue(ruleMapper.selectCount(Wrappers.lambdaQuery(StarArchivingCodeRule.class)
                .eq(StarArchivingCodeRule::getStarId, saveDTO.getStarId())
                .ne(Objects.nonNull(saveDTO.getId()),StarArchivingCodeRule::getId, saveDTO.getId())
                .eq(StarArchivingCodeRule::getIsDelete, NumberCst.ZERO)
        ) == 0, McnErrorCode.DATA_EXISTED, "该特殊红人已配置了归档编码规则");
        BusinessAssert.isTrue(ruleMapper.selectCount(Wrappers.lambdaQuery(StarArchivingCodeRule.class)
                .eq(StarArchivingCodeRule::getCodePrefix, saveDTO.getCodePrefix())
                .ne(Objects.nonNull(saveDTO.getId()),StarArchivingCodeRule::getId, saveDTO.getId())
                .eq(StarArchivingCodeRule::getIsDelete, NumberCst.ZERO)
        ) == 0, McnErrorCode.DATA_EXISTED, "编码前缀已存在");
        if(Objects.isNull(saveDTO.getId())) {
            StarArchivingCodeRule addRule = new StarArchivingCodeRule();
            addRule.setStarId(saveDTO.getStarId());
            addRule.setCodePrefix(saveDTO.getCodePrefix());
            return ruleMapper.insert(addRule) > 0;
        }
        StarArchivingCodeRule updateEntity = new StarArchivingCodeRule();
        updateEntity.setId(saveDTO.getId());
        updateEntity.setStarId(saveDTO.getStarId());
        updateEntity.setCodePrefix(saveDTO.getCodePrefix());
        return ruleMapper.updateById(updateEntity) > 0;
    }

    @Override
    public Page<StarArchivingCodeRuleVO> page(McnPage<StarArchivingCodeRule> mcnPage) {
        return ruleMapper.page(mcnPage, UserVoContextHolder.getTenantId());
    }

    @Override
    public StarArchivingCodeRuleVO detail(Long id) {
        return ruleMapper.detail(id);
    }

    @Override
    public boolean changeEditStatus(Boolean enabled) {
        stringRedisTemplate.opsForValue().getAndSet(UserVoContextHolder.getTenantId()+":"+McnConstant.CACHE_PRIFIX + "ARCHIVING_EDIT_STATUS", enabled.toString());
        return this.getEditStatus();
    }

    @Override
    public boolean getEditStatus() {
        String enabledStr = stringRedisTemplate.opsForValue().get(UserVoContextHolder.getTenantId()+":"+McnConstant.CACHE_PRIFIX + "ARCHIVING_EDIT_STATUS");
        if(StringUtils.isBlank(enabledStr)) {
            return false;
        }
        return Boolean.valueOf(enabledStr).booleanValue();
    }
}
